package org.geektimes.injection.comparator;


import org.geektimes.injection.initializer.WebApplicationContextInitializer;

import java.util.Comparator;
import java.util.Objects;

/**
 * @FileName: OrderComparator
 * @Description: TODO
 * @Author: MaWenHao
 * @Created: 2021/3/25 - 上午12:07
 */
public class OrderComparator implements Comparator<WebApplicationContextInitializer> {

    @Override
    public int compare(WebApplicationContextInitializer o1, WebApplicationContextInitializer o2) {
        Order order1 = o1.getClass().getAnnotation(Order.class);
        Order order2 = o2.getClass().getAnnotation(Order.class);
        int value1 = getValue(order1);
        int value2 = getValue(order2);
        return Integer.compare(value1, value2);
    }

    private int getValue(Order order1) {
        if (Objects.isNull(order1)) {
            return 0;
        }
        return order1.value();
    }
}
